﻿package cn.appfactory.adbanner;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.mobisage.android.MobiSageAdBanner;
import com.mobisage.android.MobiSageAdSize;
import com.mobisage.android.MobiSageAnimeType;
import com.mobisage.android.MobiSageEnviroment;

public class MainActivity extends Activity
{
    private MobiSageAdBanner adv;
    private LinearLayout  banner;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        banner = (LinearLayout) findViewById(R.id.banner);
        advertiseBanner();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(adv != null){
            adv.destroyDrawingCache();
            adv.clearDisappearingChildren();
            adv.removeAllViews();
        }
    }

    /**
     * 一般banner广告
     * 点击广告条时，需要注意配置AndroidManifest.xml,添加com.mobisage.android.MobiSageActivity
     */
    public void advertiseBanner(){

        //自定义尺寸广告  MobiSageAdSize.Size_540X80
        adv = new MobiSageAdBanner(this, MobiSageAdSize.Poster_480X800,null,null,null);
       // adv.listener

        //设置延迟时间
        adv.setAdRefreshInterval(MobiSageEnviroment.AdRefreshInterval.Ad_Refresh_15);

        //设置广告轮显效果
        adv.setAnimeType(MobiSageAnimeType.Anime_LeftToRight);
        banner.addView(adv);
    }

    /**
     * 点击返回按钮
     */
    @Override
    public void onBackPressed(){
        new AlertDialog.Builder(this)
                .setTitle("系统提示")
                .setMessage("确定要退出程序么？")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        MainActivity.this.finish();
                        if (adv != null){
                            adv.destroyDrawingCache();
                            adv.clearDisappearingChildren();
                            adv.removeAllViews();
                        }
                    }
                }).show();
    }
}
